package com.sharemarking.wa.platform.mapper;

import com.sharemarking.wa.common.entity.Member;
import com.sharemarking.wa.common.entity.RealPersonVerification;
import com.sharemarking.wa.common.entity.SigninRecord;
import com.sharemarking.wa.platform.dto.AreaDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MemberMapper {
	@Select("SELECT * FROM sys_member WHERE id=#{id} AND del_flag=0")
	Member get(Integer id);
	@Select("SELECT * FROM sys_member WHERE id=(SELECT member_id FROM wa_driver_verify b WHERE b.id = #{id} ) AND del_flag=0")
	Member getByDriverId(Integer id);
	@Select("SELECT * FROM sys_member WHERE mobile=#{mobile} AND del_flag=0")
	Member getByMobile(String mobile);
	@Update("UPDATE eu_wechat_member SET member_id=#{memberId} WHERE id=#{wechatMemberId} AND del_flag=0 ")
	void bindWechat(@Param("wechatMemberId") Integer wechatMemberId, @Param("memberId") Integer memberId);
	@Update("UPDATE eu_wechat_member SET member_id = null WHERE id=#{wechatMemberId} AND del_flag=0 ")
	void unbindWechat(@Param("wechatMemberId") int wechatMemberId);

	/**
	 * 获取Member
	 * @param id
	 * @return
	 */
	@Select("SELECT * FROM sys_member WHERE id = #{id} and del_flag = 0")
	Member findMemberById(Integer id);

	/**
	 * 获取Member
	 * @param mobile
	 * @return
	 */
	@Select("SELECT * FROM sys_member WHERE mobile = #{mobile} and del_flag = 0")
	Member findMemberByMobile(String mobile);

	/**
	 * 用户注册
	 * @param member
	 */
	@Insert("INSERT INTO sys_member(nick_name,mobile,password,head_img,create_by,create_date,update_by,role,real_name)" +
	         " value (#{nickName},#{mobile},#{password},#{headImg},#{createBy},#{createDate},#{updateBy},#{role},#{realName})")
	@Options(useGeneratedKeys=true, keyProperty="id",keyColumn = "id")
	void register(Member member);

	/**
	 * h5用户注册
	 * @param member
	 */
	@Insert("INSERT INTO sys_member(type,mobile,password,name,head_img_url,create_by,create_date,update_by)" +
			" value (#{type},#{mobile},#{password},#{name},#{headImgUrl},#{createBy},#{createDate},#{updateBy})")
	@Options(useGeneratedKeys=true, keyProperty="id",keyColumn = "id")
	void h5Register(Member member);


	/**
	 * 更新用户信息
	 * @return
	 */
	@Update("UPDATE sys_member set head_img = #{headImg},nick_name = #{nickName},update_date = #{updateDate} WHERE id = #{id}")
	void updateMember(Member member);
	/**
	 * 修改member手机号码
	 * @param id
	 * @param mobile
	 */
	@Update("UPDATE sys_member SET mobile = #{mobile},update_date = #{updateDate} WHERE id = #{id}")
	void updateMobile(Member member);
	/**
	 * 修改member手机号码和类型
	 * @param id
	 * @param mobile
	 */
	@Update("UPDATE sys_member SET type=#{type},mobile = #{mobile},update_date = #{updateDate},type=#{type} WHERE id = #{id}")
	void updateMobileAndType(Member member);

	@Update("set NAMES 'utf8mb4'")
	void setNameUtf8Mb4();

	@Delete("DELETE FROM sys_member WHERE id = #{id}")
	void deleteMember(Integer id);


	@Select("SELECT img_url FROM eu_comment_pic WHERE comment_id = #{id}")
	List<String> getCommentPicList(Integer id);

	@Select("SELECT * FROM eu_real_person_verification WHERE member_id = #{memberId} AND type = 1")
	RealPersonVerification getVail(Integer memberId);

	@Insert("INSERT INTO eu_real_person_verification"
			+ "(type,member_id,name_input,ID_number_input,real_pic,ID_card_front,ID_card_back,create_date)"
			+ "VALUES (#{type},#{memberId},#{nameInput},#{idNumberInput},#{realPic},#{idCardFront},#{idCardBack},#{createDate}) ")
	@Options(useGeneratedKeys=true, keyProperty="id",keyColumn = "id")
	int insertRealPersonVerification(RealPersonVerification realPersonVerification);

	@Insert("UPDATE eu_real_person_verification SET face_token = #{faceToken},real_pic = #{realPic},update_date = #{updateDate},"
			+ "state = #{state},reject_reason = #{rejectReason} WHERE id = #{id}")
	@Options(useGeneratedKeys=true, keyProperty="id",keyColumn = "id")
	int updateRealPersonVerification(RealPersonVerification realPersonVerification);

	@Delete("DELETE FROM eu_real_person_verification WHERE id = #{id}")
	void deleteVail(RealPersonVerification vail);

	@Select("SELECT * FROM eu_signin_record WHERE qr_code = #{qrCode} or rfid_code = #{qrCode}")
	List<SigninRecord> getSigninRecordByQrCode(String qrCode);


	@Update("update sys_member set password = #{password} where id = #{id}")
	void updatePsw(@Param("id") Integer id, @Param("password") String password);
	
	@Update("UPDATE eu_competition_people SET face_state = 0 WHERE member_id = #{id}")
	void updateCompetitionPeopleFaceState(Integer id);

	@Select("SELECT role FROM sys_member WHERE id = #{memberId}")
	Integer getRoleByMemberId(Integer memberId);


	@Update("UPDATE sys_member SET pay_password = #{payPassword},update_date = #{updateDate} WHERE id = #{id}")
	void updateMemberPayPwd(Member member);

	List<AreaDto> getAreaList(@Param("code") String code);

	@Update("UPDATE sys_member SET real_name = #{realName},update_date = #{updateDate} WHERE id = #{id}")
	void setRealName(Member member);

	@Select("SELECT pay_password FROM sys_member WHERE id =#{memberId}")
	String getPayPwdByMemberId(Integer memberId);

	@Select("SELECT alias FROM sys_member WHERE id = #{memberId}")
	String getAlias(Integer memberId);

	@Select("SELECT IFNULL(real_name,nick_name) FROM sys_member WHERE id = #{memberId}")
	String getRealNameById(Integer memberId);

	@Select("SELECT id FROM sys_member")
	List<Integer> getIdList();

	@Update("UPDATE sys_member SET alias = #{alias} WHERE id = #{id}")
	void updateAlias(@Param("id")Integer id, @Param("alias") String uuid);

	@Update("UPDATE sys_member SET real_name = null WHERE id = #{memberId}")
	void updateRealNameNull(Integer memberId);
}
